package com.love.marriage.userPartnerPreferences.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.oss.service.FileInfoService;
import com.love.marriage.userPartnerPreferences.entity.UserPartnerPreferences;
import com.love.marriage.userPartnerPreferences.mapper.UserPartnerPreferencesMapper;
import com.love.marriage.userPartnerPreferences.service.UserPartnerPreferencesService;
import com.love.marriage.userPartnerPreferences.vo.UserPartnerPreferencesVo;
import com.love.marriage.userPartnerPreferences.vo.UserPartnerVo;
import com.love.marriage.userProfileInfo.mapper.UserProfileInfoMapper;
import com.love.marriage.userProfileInfo.vo.UserProfileInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户择偶要求表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-29 22:13:42
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserPartnerPreferencesServiceImp extends ServiceImpl<UserPartnerPreferencesMapper, UserPartnerPreferences> implements UserPartnerPreferencesService {

    private final UserPartnerPreferencesMapper userPartnerPreferencesMapper;

    private final UserProfileInfoMapper userProfileInfoMapper;

    private final FileInfoService fileInfoService;

    @Override
    public Page<UserPartnerPreferencesVo> getPage(Long pageNum, Long pageSize, UserPartnerPreferencesVo userPartnerPreferencesVo) {
        Page<UserPartnerPreferencesVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userPartnerPreferencesMapper.getPage(page, userPartnerPreferencesVo);
    }

    @Override
    public UserPartnerPreferencesVo queryUserPartnerPreferences(Long id) {
        return userPartnerPreferencesMapper.queryUserPartnerPreferences(id);
    }

    @Override
    public UserPartnerPreferencesVo queryUserPartnerPreferencesByUserId(Long userId) {
        return userPartnerPreferencesMapper.queryUserPartnerPreferencesByUserId(userId);
    }

    @Override
    public UserPartnerPreferencesVo addUserPartnerPreferences(UserPartnerPreferencesVo userPartnerPreferencesVo) {
        UserPartnerPreferencesVo dbUserInfo = queryUserPartnerPreferencesByUserId(userPartnerPreferencesVo.getUserId());
        if (dbUserInfo == null) {
            dbUserInfo = new UserPartnerPreferencesVo();
        }
        BeanUtils.copyProperties(userPartnerPreferencesVo, dbUserInfo, "id");
        UserPartnerPreferences userPartnerPreferences = new UserPartnerPreferences();
        BeanUtils.copyProperties(dbUserInfo, userPartnerPreferences);
        if (dbUserInfo.getId() == null) {
            userPartnerPreferencesMapper.insert(userPartnerPreferences);
        } else {
            userPartnerPreferencesMapper.updateById(userPartnerPreferences);
        }
        UserPartnerPreferencesVo res = new UserPartnerPreferencesVo();
        BeanUtils.copyProperties(userPartnerPreferences, res);
        return res;
    }

    @Override
    public UserPartnerPreferencesVo updateUserPartnerPreferences(UserPartnerPreferencesVo userPartnerPreferencesVo) {
        UserPartnerPreferences userPartnerPreferences = new UserPartnerPreferences();
        BeanUtils.copyProperties(userPartnerPreferencesVo, userPartnerPreferences);
        userPartnerPreferencesMapper.updateById(userPartnerPreferences);
        return userPartnerPreferencesVo;
    }

    @Override
    public Boolean deleteUserPartnerPreferences(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userPartnerPreferencesMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    public List<UserPartnerVo> queryReferenceList(Long userId) {
        return queryReferenceList(userId, 3);
    }

    @Override
    public List<UserPartnerVo> queryReferenceList(Long userId, Integer minScore) {
        String searchGender = null;
        if (userId != null) {
            UserProfileInfoVo userProfileInfoVo = userProfileInfoMapper.queryUserProfileInfoByUserId(userId);
            if (userProfileInfoVo != null && userProfileInfoVo.getGender() != null) {
                switch (userProfileInfoVo.getGender()) {
                    case "0":
                        searchGender = "1";
                        break;
                    case "1":
                        searchGender = "0";
                        break;
                }
            }
        }
        List<UserPartnerVo> userPartnerVos = userPartnerPreferencesMapper.selectPartnerInfoWithMinScore(userId, searchGender, minScore);
        if (userPartnerVos != null && !userPartnerVos.isEmpty()) {
            userPartnerVos.forEach(item -> {
                if (item.getAvatar() != null) {
                    try {
                        item.setPersonAvatar(fileInfoService.queryFileInfo(item.getAvatar()));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        return userPartnerVos;
    }
}
